package jdesktop.drag;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ConnectedComponentTest
{
    public static void main(String[] args)
    {
        JFrame master = FrameTest.createFrame();
        master.setBounds(100, 100, 200, 300);
        final JWindow slave = new JWindow();
        GraphicsConfiguration gc = slave.getGraphicsConfiguration();
        Graphics g = slave.getGraphics();
        
        slave.getRootPane().setBorder(new MatteBorder(10,10,10,10, Color.BLUE));
        slave.getRootPane().setOpaque(false);
        slave.getRootPane().setBackground(null);
        slave.getRootPane().getContentPane().setBackground(null);
        slave.setBackground(null);
        
        slave.setBounds(10, 10, 20, 400);
        slave.setVisible(true);
        master.setTitle("master");

        final ConnectedComponent cc = ConnectedComponent.connect(ConnectedComponent.Axis.Y, master, slave);
        cc.setPlacement(-1.0);

        final JTextField number = new JTextField("");
        number.setVisible(true);
        number.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String text = number.getText();
                try
                {
                    double d = Double.parseDouble(text);
                    cc.setPlacement(d);
                }
                catch (NumberFormatException ex)
                {
                }
            }
        });
        number.setSize(100,50);
        master.add(number);
        master.invalidate();

        final Rectangle bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        slave.addComponentListener(new ComponentAdapter()
        {
            public void componentResized(ComponentEvent e)
            {
                checkSlavePlacement();
            }

            public void componentMoved(ComponentEvent e)
            {
                checkSlavePlacement();
            }

            private void checkSlavePlacement()
            {
                Rectangle pos = slave.getBounds();
                Rectangle intersect = pos.intersection(bounds);
                // todo - use axis to determine orientation
                int diff= pos.height-intersect.height;
                if (diff>0)
                {
                    double placementFlip = cc.getPlacement()*-1.0;

                    pos = cc.computeSlavePosition(placementFlip);
                    intersect = pos.intersection(bounds);
                    int flipDiff = pos.height-intersect.height;
                    if (flipDiff<diff)
                        cc.setPlacement(placementFlip);
                }
            }

        });

    }
}
